
import {
  bookCategoryList
} from "../../../utils/commondata.js";

import {
  request
} from "../../../utils/request.js";
import regeneratorRuntime from '../../../utils/runtime';

var appInst =  getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    mainColor:appInst.globalData.mainColor,
    bookCategoryList:bookCategoryList(),
    curBookThemeIndex:0,
    bookTypeList:bookCategoryList()[0].typeList,
    curBookTypeIndex:0,
    bookList:[],
    page:1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadBookList(1,this.data.bookTypeList[0]);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageNo = this.data.page + 1;
    let bookType = this.data.bookTypeList[this.data.curBookTypeIndex];
    this.loadBookList(pageNo,bookType);
  },
  /**
   * 主题选择
   * 
   * @param {*} e 
   */
  themeSelect(e){
    let {index} =  e.currentTarget.dataset;
    console.log(index);
    let bookTypeList = bookCategoryList()[index].typeList;
    this.setData({
      curBookThemeIndex:index,
      curBookTypeIndex:0,
      bookTypeList:bookTypeList,
      page:1,
      bookList:[],
    });
    this.loadBookList(1,bookTypeList[0]);
  },
  /**
   * 选择类型
   * @param {*} e 
   */
  handleSelectType(e){
    let {index} =  e.currentTarget.dataset;
    let bookType = this.data.bookTypeList[index];
    this.setData({
      curBookTypeIndex:index,
      bookList:[],
    });
    this.loadBookList(1,bookType);
  },
  /**
   * 跳转到明细
   * 
   * @param {*} e 
   */
  handleDetail(e){
    let {id,name} = e.currentTarget.dataset;
    console.log(id);
    wx.navigateTo({
      url:'/pages/book/detail/index?bookId=' + id + "&bookName=" + name,
    });
  },
  /**
   * 
   * 加载书本列表
   * @param {*} page 
   * @param {*} type 
   */
  async loadBookList(pageNo,typeName){
    const res = await request({
      url: "/book/queryPage?limit=10&typeName=" + typeName.value + "&page=" + pageNo,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    this.setData({
      bookList:[...this.data.bookList,...res.data.list],
      page:pageNo,
    });
  },

})